﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Resources;
using System.Text;
using System.Windows.Forms;
using SMS.cntr;
using SMS.dao;
using SMS.util;

namespace SMS.view
{
    public partial class CreateCourseForm : Form
    {
        private int courseId;

        public CreateCourseForm()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            CourseCntr.Instance.insert(txtCourse.Text);
            bindDataToGridViewCourse();
            txtCourse.Text = "";
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            CourseCntr.Instance.update(txtCourse.Text, courseId);
            bindDataToGridViewCourse();
            txtCourse.Text = txtCourse.Text;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            CourseCntr.Instance.delete(courseId);
            bindDataToGridViewCourse();
            txtCourse.Text = "";
        }

        private void CreateCourseForm_Load(object sender, EventArgs e)
        {
            bindDataToGridViewCourse();
            changeLang(I18nUtil.vnLang);
        }

        private void bindDataToGridViewCourse()
        {
            gridViewCourse.Rows.Clear();
            DataTable table = CourseCntr.Instance.findAll();

            for (int i = 0; i < table.Rows.Count; i++)
            {
                gridViewCourse.Rows.Add(i + 1,
                                           table.Rows[i]["cor_name"].ToString(),
                                           table.Rows[i]["cor_id"].ToString());
                gridViewCourse.Rows[gridViewCourse.RowCount - 1].Tag = table.Rows[i];
            }
            gridViewCourse.ClearSelection();
        }

        private void gridViewCourse_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            for (int i = 0; i < gridViewCourse.SelectedRows.Count; i++)
            {
                String id = gridViewCourse.SelectedRows[i].Cells[2].Value.ToString();
                String course = CourseDao.Instance.findById(Int32.Parse(id))[1].ToString();
                courseId = Int32.Parse(CourseDao.Instance.findById(Int32.Parse(id))[0].ToString());
                txtCourse.Text = course;
            }
        }

        public void changeLang(string lang)
        {
            I18nUtil.init(lang, new ResourceManager(I18nUtil.resourcePath, typeof(CreateCourseForm).Assembly));

            this.Text = I18nUtil.getString("createCourse");
            label1.Text = I18nUtil.getString("lblCourse");
            groupBox1.Text = I18nUtil.getString("gbCourseList");
            btnAdd.Text = I18nUtil.getString("btnAdd");
            btnUpdate.Text = I18nUtil.getString("btnUpdate");
            btnDelete.Text = I18nUtil.getString("btnDelete");
            gridViewCourse.Columns[0].HeaderText = I18nUtil.getString("no");
            gridViewCourse.Columns[1].HeaderText = I18nUtil.getString("courseName");
        }


    }
}
